package com.keepc.licence.util;

import de.schlichtherle.license.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.security.auth.x500.X500Principal;

import com.keepc.licence.entity.LicenseCreatorParam;
import com.keepc.licence.serverinfo.CustomKeyStoreParam;

import java.io.File;
import java.text.MessageFormat;
import java.util.prefs.Preferences;

/**
 * 证书生成器
 */
@Data
@Slf4j
public class LicenseCreator {

    private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal(
            "CN=keepc, OU=keepc, O=keepc, L=KC, ST=KC, C=CN");

    private LicenseCreatorParam param;

    public LicenseCreator(LicenseCreatorParam param) {
        this.param = param;
    }

    /**
     * 生成License证书
     *
     * @return boolean
     */
    public boolean generateLicense() {
        try {
            LicenseManager licenseManager = new LicenseManager(initLicenseParam());
            LicenseContent licenseContent = initLicenseContent();

            licenseManager.store(licenseContent, new File(param.getLicensePath()));

            return true;
        } catch (Exception e) {
            log.error(MessageFormat.format("证书生成失败：{0}", param), e);
            return false;
        }
    }

    /**
     * 初始化证书生成参数
     *
     * @return de.schlichtherle.license.LicenseParam
     */
    private LicenseParam initLicenseParam() {
        Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);
        // 设置对证书内容加密的秘钥
        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());
        KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class,
                param.getPrivateKeysStorePath(), param.getPrivateAlias(),
                param.getStorePass(), param.getKeyPass());
        return new DefaultLicenseParam(param.getSubject(), preferences,
                privateStoreParam, cipherParam);
    }

    /**
     * 设置证书生成正文信息
     *
     * @return de.schlichtherle.license.LicenseContent
     */
    private LicenseContent initLicenseContent() {
        LicenseContent licenseContent = new LicenseContent();
        licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);

        licenseContent.setSubject(param.getSubject());
        licenseContent.setIssued(param.getIssuedTime());
        licenseContent.setNotBefore(param.getIssuedTime());
        licenseContent.setNotAfter(param.getExpireTime());
        licenseContent.setConsumerType(param.getConsumerType());
        licenseContent.setConsumerAmount(param.getConsumerAmount());
        licenseContent.setInfo(param.getDescription());

        // 扩展校验服务器硬件信息
        licenseContent.setExtra(param.getAdditionInfo());

        return licenseContent;
    }
}
